package com.liugh.controller.zhongche;

import com.baomidou.mybatisplus.plugins.Page;
import com.liugh.annotation.CurrentUser;
import com.liugh.base.BusinessException;
import com.liugh.base.PublicResultConstant;
import com.liugh.config.ResponseModel;
import com.liugh.controller.BaseRestController;
import com.liugh.entity.User;
import com.liugh.entity.standard.StandardPark;
import com.liugh.model.DownloadModel;
import com.liugh.service.zhongche.IStandardParkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Fatao on 2018/11/5 .
 */
@RestController
@Api(description = "标准化园地")
@RequestMapping("/standard_park")
public class StandardParkController extends BaseRestController {

    @Autowired
    private IStandardParkService standardParkService;


    /**
     * 添加一个园地 .
     * @param standardPark .
     * @return .
     * @throws BusinessException .
     */
    @ApiOperation("添加一个园地")
    @PostMapping("/add")
    public ResponseModel<StandardPark> addPark(@RequestBody StandardPark standardPark) throws BusinessException {
        StandardPark park = standardParkService.addPark(standardPark);
        if (park == null) {
            throw new BusinessException(PublicResultConstant.ERROR);
        }
        return fillSuccessData(park);
    }




    /**
     * 更新一个园地 .
     * @param standardPark .
     * @return .
     * @throws BusinessException .
     */
    @ApiOperation("更新一个园地")
    @PutMapping("/update")
    public ResponseModel<StandardPark> updatePark(@RequestBody StandardPark standardPark) throws BusinessException {
        StandardPark park = standardParkService.updatePark(standardPark);
        if (park == null) {
            throw new BusinessException(PublicResultConstant.ERROR);
        }
        return fillSuccessData(park);
    }


    /**
     * 根据id删除park .
     * @param id .
     * @return .
     */

    @ApiOperation("根据id删除park")
    @DeleteMapping("/delete")
    public ResponseModel<String> delParkById(@RequestParam("id") int id) {
        if (standardParkService.delParkById(id) > 0) {
            return fillSuccessData(PublicResultConstant.SUCCEED);
        } else {
            return fillSuccessData(PublicResultConstant.ERROR);
        }
    }


    /**
     * 获取所有的park .
     * @param pageNum .
     * @param pageSize .
     * @return .
     */

    @ApiOperation("获取所有的park")
    @GetMapping("get_all")
    public ResponseModel<Page<StandardPark>> getAllPark(@RequestParam("pageNum") int pageNum,
                                                        @RequestParam("pageSize") int pageSize) {
        return fillSuccessDataPage(standardParkService.getAllPark(new Page<>(pageNum, pageSize)));
    }

    /**
     * 获取我创建的park .
     * @param pageNum .
     * @param pageSize .
     * @param user .
     * @return .
     */
    @ApiOperation("获取我创建的park")
    @GetMapping("get_my")
    public ResponseModel<Page<StandardPark>> getMyPark(@RequestParam("pageNum") int pageNum,
                                                       @RequestParam("pageSize") int pageSize,
                                                       @CurrentUser User user) {
        return fillSuccessDataPage(standardParkService.getMyPark(new Page<>(pageNum, pageSize), user.getUserNo()));
    }


    @ApiOperation("下载园地附件")
    @ApiImplicitParam(name = "id",value = "StandardPark的ID",paramType = "query",dataType = "int")
    @GetMapping("/download")
    public ResponseModel<DownloadModel> getPathAndFilename(@RequestParam("id")int id) throws BusinessException {
        DownloadModel downloadModel = standardParkService.getPathAndFilename(id);
        if (downloadModel == null) {
            throw new BusinessException(PublicResultConstant.FILE_NOT_EXIST);
        }
        return fillSuccessData(downloadModel);
    }
}
